/*
 * ipassbook 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.dialog.wizard;

import java.awt.Component;
import java.awt.event.ActionEvent;

import com.wateray.ipassbook.domain.Passbook;
import com.wateray.ipassbook.domain.PassbookDetail;
import com.wateray.ipassbook.domain.form.PassbookForm;
import com.wateray.ipassbook.kernel.event.IPassbookEvent;
import com.wateray.ipassbook.kernel.event.IPassbookEventType;
import com.wateray.ipassbook.kernel.event.IPassbookObserver;
import com.wateray.ipassbook.kernel.service.PassbookService;
import com.wateray.ipassbook.util.LanguageLoader;

/**
 * @author wateray
 * @create 2009-5-16
 */
public class PassbookWizardDialog extends AbstractWizardDialog {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private static int width = 480;
	private static int height = 380;

	private PassbookWizardPageOne pageOne;
	private PassbookWizardPageTwo pageTwo;
	private PassbookWizardPageThree pageThree;

	private PassbookForm entityForm;

	public PassbookWizardDialog(Component owner) {
		this(owner, width, height, true, LanguageLoader
				.getString("Passbook_Wizard"));
	}

	public PassbookWizardDialog(Component owner, String title) {
		this(owner, width, height, true, title);
		// TODO Auto-generated constructor stub
	}

	public PassbookWizardDialog(Component owner, int width, int height,
			boolean modal, String title) {
		super(owner, width, height, modal, title);

		initComponent();
		initData();
	}

	private void initComponent() {
		pageOne = new PassbookWizardPageOne(this);
		pageTwo = new PassbookWizardPageTwo(this);
		pageThree = new PassbookWizardPageThree(this);

		this.addPage(pageOne);
		this.addPage(pageTwo);
		this.addPage(pageThree);

		// set finishButton enable.
//		this.setCanFinishPageCount(2);

		this.finishAddPage();
	}

	private void finishProcessData(){
		Passbook passbook = new Passbook();
		PassbookDetail passbookDetail = new PassbookDetail();
		passbook.setPassbookName(entityForm.getPassbookName());
		passbook.setCreateDate(entityForm.getCreateDate());
		passbook.setMemo(entityForm.getMemo());
		
		passbookDetail.setCurrencyId(entityForm.getCurrencyId());
		passbookDetail.setCapital(entityForm.getCapital());
		passbookDetail.setBalance(entityForm.getCapital());
		passbookDetail.setMemo(entityForm.getMemo());
		
		PassbookService service = new PassbookService();
		int result = service.insertPassbookDetail(passbook, passbookDetail);
		if(result >0 ){
			//notify IPassbookEvent
			IPassbookObserver.getInstance().notifyEvents(new IPassbookEvent(this,IPassbookEventType.PassbookTableAddEvent));
		}	
	  this.dispose();
		
	}
	
	@Override
	protected void initData() {
		entityForm = new PassbookForm();
		this.setEntity(entityForm);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.wateray.ipassbook.ui.widget.TitleMessageDialog#
	 * finishButtonActionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	protected void finishButtonActionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
    	if(checkData()){
    		//processData
    		processData();
    	}
    	
    	//
    	finishProcessData();
    	
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wateray.ipassbook.kernel.event.IPassbookListener#update(com.wateray
	 * .ipassbook.kernel.event.IPassbookEvent)
	 */
	@Override
	public void update(IPassbookEvent event) {
		// TODO Auto-generated method stub
        
	}


}
